package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.IProjectService;
import cn.edu.seu.ssny.service.IRoleService;
import cn.edu.seu.ssny.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MainController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IProjectService projectService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = {"/main"})
    public String index(HttpSession session, ModelMap map) {
        User currUser = (User) session.getAttribute("currUser");
        Enterprise enterprise = (Enterprise) session.getAttribute("enterprise");
        List<Project> projects;
        if (currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL) {
            projects = new ArrayList<>(currUser.getProjects());
        } else {
            projects = projectService.findAllByEnterprise(enterprise);
        }
        Collections.sort(projects);
        session.setAttribute("projects", projects);
//        map.put("projects", projects);
        return "main";
    }

    @RequestMapping(value = {"/previlige/no"})
    public String previlige(HttpSession session, ModelMap map) {
        return "error/previlige";
    }

}
